"""
定义视图，响应登陆请求
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from meiduo_admin.serializers.login_serializers import LoginSerializer

class LoginAPIView(APIView):

    def post(self, request):
        # 使用序列化器，完成身份校验，签发token
        # 1、获取前端传来的被校验的数据
        credentials = request.data
        # 2、构建序列化器对象，通过data参数传入被校验数据
        serializer = LoginSerializer(data=credentials)
        # 3、启动校验流程
        serializer.is_valid(raise_exception=True)
        # 4、获取有效数据，并构建响应对象(构建响应数据)
        return Response(data={
            "username": serializer.validated_data['user'].username,
            "user_id": serializer.validated_data['user'].id,
            "token": serializer.validated_data['token']
        })













